from ev import Command, CmdSet
from src.commands.default.muxcommand import MuxCommand
from game.gamesrc.commands.command import MyCommand


class CmdSetWild(CmdSet):
    "CmdSet for the lightsource commands"
    key = "lightsource_cmdset"
    priority = 1
    def at_cmdset_creation(self):
        "called at cmdset creation"
        self.add(CmdNorth())
        self.add(CmdWest())
        self.add(CmdEast())
        self.add(CmdSouth())
        self.add(CmdUp())
        self.add(CmdDown())
        self.add(CmdWReload())
        self.add(CmdWSet())



class CmdDirections(Command):
    """
    This is the template class for all direction commands.

    """
    locks = "cmd:all()"

    def func(self):
        "implements the command."
        self.caller.msg("Non puoi andare in quella direzione.")

class CmdDown(CmdDirections):
    """
    down

    Usage:
      down

    Go down.

    """
    key = "down"
    aliases = ["d", "giu"]

class CmdEast(CmdDirections):
    """
    east

    Usage:
      east

    Go east.

    """
    key = "east"
    aliases = [ "e", "est"]

    def func(self):
        caller = self.caller
        old_loc = caller.db.wild_loc
        new_loc = list(old_loc)
        new_loc[0] += 1
        new_loc = tuple(new_loc)
        ok = caller.wild_move_to(caller.location, new_loc)
        if not ok:
            caller.msg("Non puoi andare in quella direzione.")
        else:
            caller.msg("Vai verso est.")
            caller.location.msg_contents("%s va' verso est." % caller.name, exclude=caller, wild_loc=old_loc)
            caller.location.msg_contents("%s arriva da ovest." % caller.name, exclude=caller, wild_loc=new_loc)
            caller.execute_cmd('look')

class CmdNorth(CmdDirections):
    """
    north

    Usage:
      north

    Go north.

    """
    key = "north"
    aliases = ["n", "nord"]

    def func(self):
        caller = self.caller
        old_loc = caller.db.wild_loc
        new_loc = list(old_loc)
        new_loc[1] += -1
        new_loc = tuple(new_loc)
        ok = caller.wild_move_to(caller.location, new_loc)
        if not ok:
            caller.msg("Non puoi andare in quella direzione.")
        else:
            caller.msg("Vai verso nord.")
            caller.location.msg_contents("%s va' verso nord." % caller.name, exclude=caller, wild_loc=old_loc)
            caller.location.msg_contents("%s arriva da sud." % caller.name, exclude=caller, wild_loc=new_loc)
            caller.execute_cmd('look')


class CmdSouth(CmdDirections):
    """
    south

    Usage:
      south

    Go south.

    """
    key = "south"
    aliases = ["s", "sud"]

    def func(self):
        caller = self.caller
        old_loc = caller.db.wild_loc
        new_loc = list(old_loc)
        new_loc[1] += +1
        new_loc = tuple(new_loc)
        ok = caller.wild_move_to(caller.location, new_loc)
        if not ok:
            caller.msg("Non puoi andare in quella direzione.")
        else:
            caller.msg("Vai verso sud.")
            caller.location.msg_contents("%s va' verso sud." % caller.name, exclude=caller, wild_loc=old_loc)
            caller.location.msg_contents("%s arriva da nord." % caller.name, exclude=caller, wild_loc=new_loc)
            caller.execute_cmd('look')

class CmdWest(CmdDirections):
    """
    west

    Usage:
      west

    Go west.

    """
    key = "west"
    aliases = ["w", "ovest", "o"]

    def func(self):
        caller = self.caller
        old_loc = caller.db.wild_loc
        new_loc = list(old_loc)
        new_loc[0] += -1
        new_loc = tuple(new_loc)
        ok = caller.wild_move_to(caller.location, new_loc)
        if not ok:
            caller.msg("Non puoi andare in quella direzione.")
        else:
            caller.msg("Vai verso ovest.")
            caller.location.msg_contents("%s va' verso ovest." % caller.name, exclude=caller, wild_loc=old_loc)
            caller.location.msg_contents("%s arriva da est." % caller.name, exclude=caller, wild_loc=new_loc)
            caller.execute_cmd('look')


class CmdUp(CmdDirections):
    """
    up

    Usage:
      up

    Go up.

    """
    key = "up"
    aliases = ["u", "su"]


class CmdWReload(MyCommand):
    """
    @wreload - reload the wilderness

    Usage:
        @wreload <layer>

    Reload this wilderness from files.
    If no layer is given, the wilderness will be fully reloaded.
    All variables stored are keeped, so it's safer to use this 
    instead of @type/reset.
    """

    key = "@wreload"
    aliases = ["@wr"]
    locks = "cmd: perm(wreload) or perm(Builders)"
    help_category = "Building"

    def func(self):
        args = self.args
        arg = None
        if args: arg = self.args[0][1]
        caller = self.caller
        wild = caller.location
        if not arg:
            for element in wild.layers:
                wild.layers[element]._reload()
                caller.msg("Ricaricato il layer '%s'" % element)
        elif arg in wild.layers:
            wild.layers[arg]._reload()
            caller.msg("Ricaricato il layer '%s'" % arg)
        else:
            caller.msg("Impossibile trovare il layer %s" % arg)

class CmdWSet(MuxCommand):
    """
    @wset - set a variables in a wilderness layer.

    Usage:
        @wset <layer_name> [= string]

    """

    key = "@wset"
    aliases = ["@ws"]
    locks = "cmd: perm(wset) or perm(Builders)"
    help_category = "Building"

    def func(self):
        caller = self.caller
        if not self.args or not self.lhs:
            string = "Usage: @wset <layer_name> [= string]"
            caller.msg(string)
            return
        layers = dict(caller.location.db.layers)
        layer = layers.get(self.lhs, None)
        if not layer:
            caller.msg("Non esiste un layer con questo nome: %s" % self.lhs)
            return
        if not self.rhs:
            var = layer.vars.get(caller.db.wild_loc, None)
            if not var:
                caller.msg("Nessun dato salvato nel layer %s, per la posizione %s." % (layer, caller.db.wild_loc))
                return
            if self.rhs == None:
                caller.msg("Variabile del layer %s per la posizione %s = %s." % (layer, caller.db.wild_loc, var))
                return
            else:
                del layer.vars[caller.db.wild_loc]
                layers[self.lhs] = layer
                caller.location.db.layers = layers
                caller.msg("Variabile eliminata %s, nel layer %s, per la posizione %s." % (var, layer, caller.db.wild_loc))
                return
        layer.vars[caller.wild_loc] = self.rhs
        layers[self.lhs] = layer
        caller.location.db.layers = layers
        caller.msg("Variabile salvata nel layer %s, per la posizione %s." % (layer, caller.db.wild_loc))
            
        



